package com.cgycms.webdeploy.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cgycms.webdeploy.aop.Auth;
import com.cgycms.webdeploy.aop.Log;
import com.cgycms.webdeploy.auth.AuthLoad;
import com.cgycms.webdeploy.auth.MenuEnum;
import com.cgycms.webdeploy.auth.RolesEnum;
import com.cgycms.webdeploy.common.Result;
import com.cgycms.webdeploy.log.LogModel;
import com.cgycms.webdeploy.storage.DeployConfig;
import com.cgycms.webdeploy.storage.ServersPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName : DataSourceController
 * @Description : 数据管理
 * @Author : Wlc
 * @Date: 2020-11-11 16:40
 */
@RestController
public class DataBaseController extends BaseController {

    @Autowired
    private DeployConfig deployConfig;

    @Autowired
    private AuthLoad authLoad;

    @Autowired
    private ServersPool serversPool;

    /**
     * type=1 登录/操作
     * 日志列表
     *
     * @return
     */
    @RequestMapping("/getOperationData")
    @Auth(value = "重新加载用户信息", roles = {RolesEnum.ADMIN, RolesEnum.AUDIT_ADMIN}, menus = {MenuEnum.VIEW})
    @ResponseBody
    public String getServerData(@RequestParam(required = false, value = "type") int type) {
        if (StrUtil.isBlank(deployConfig.getLogPath())) {
            return Result.ok();
        }
        List<String> logData = FileUtil.readUtf8Lines(deployConfig.getLogPath());
        Collections.reverse(logData);
        List<LogModel> result = new ArrayList<>();
        logData.forEach(s -> {
            LogModel logModel = JSONUtil.toBean(s, LogModel.class);
            if (logModel.getType() == type) {
                if (Objects.nonNull(logModel)) {
                    result.add(logModel);
                }
            }
        });
        return Result.ok(result);
    }

    /**
     * 重新加载用户信息
     *
     * @return
     */
    @RequestMapping("/refreshUserData")
    @Log(value = "重新加载用户信息操作", type = 2)
    @Auth(value = "重新加载用户信息", roles = {RolesEnum.ADMIN, RolesEnum.AUDIT_ADMIN}, menus = {MenuEnum.DELETE})
    @ResponseBody
    public String refreshUserData() {
        return Result.ok(authLoad.loadAuthUsers());
    }

    /**
     * 重新加载服务信息
     *
     * @return
     */
    @RequestMapping("/refreshServerData")
    @Log(value = "重新加载服务信息操作", type = 2)
    @Auth(value = "重新加载服务信息", roles = {RolesEnum.ADMIN, RolesEnum.AUDIT_ADMIN}, menus = {MenuEnum.DELETE})
    @ResponseBody
    public String refreshServerData() {
        return Result.ok(serversPool.loadServer(true));
    }

    /**
     * 清空日志文件信息
     *
     * @return
     */
    @RequestMapping("/deleteLogFiles")
    @Log(value = "清空日志文件信息操作", type = 2)
    @Auth(value = "清空日志文件信息", roles = {RolesEnum.ADMIN, RolesEnum.AUDIT_ADMIN}, menus = {MenuEnum.DELETE})
    @ResponseBody
    public String deleteLogFile() {
        return Result.ok(FileUtil.del(deployConfig.getLogPath()));
    }

    /**
     * 清空临时目录信息
     *
     * @return
     */
    @RequestMapping("/deleteTempFiles")
    @Log(value = "清空临时目录信息操作", type = 2)
    @Auth(value = "清空临时目录信息", roles = {RolesEnum.ADMIN, RolesEnum.AUDIT_ADMIN}, menus = {MenuEnum.DELETE})
    @ResponseBody
    public String deleteTempFiles() {
        return Result.ok(FileUtil.del(deployConfig.getTempPath()));
    }

}
